#include "tools.h"

#define BOOK_PATH "aa/"

//获取指令
int get_cmd(char start,char end)
{
    
	puts("******************");
	printf("请输入指令:");
	while(1)
	{
		int cmd =getch();
		if(start <= cmd && cmd <= end)
		{
			printf("%c\n",cmd);
			return cmd;
		}
	}
}


//获取字符串
char* get_str(char* str,size_t size)
{
	assert(NULL!=str && size>1);
	size_t len;
	while(1)
	{
		len = strlen(fgets(str,size,stdin));
		if(1==len)
		{
			printf("\n输入不能为空，请重新输入：");
		}
		else
		{
			break;
		}
	}
	if('\n' == str[len-1])
	{
		str[len-1] = '\0';
	}
	else
	{
		while('\n' != getchar());
	}
	return str;
}

//延时显示
void show_msg(const char* msg,float sec)
{
	printf("\033[01;32m%s\033[00m\n",msg);
	usleep(sec*1000000);
}


//按任意键继续
void anykey_continue(void)
{
    stdin->_IO_read_ptr = stdin->_IO_read_end;
	puts("\n按任意键继续...");
	getch();

	
}

//获取密码
char* get_passwd(char* passwd,size_t size)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;

	int i = 0;
	while(i < size-1)
	{
		passwd[i] = getch();
		// 读取到退格键
		if(127 == passwd[i])
		{
			// 数组中有已输入密码
			if(i > 0)
		    {
		    	// 删除一位密码
		    	i--;
		    	printf("\b \b");
	    	}
	    	continue;
	    }
		i++;
		printf("*");
	}
	passwd[size-1] = '\0';
	printf("\n");
	return passwd;
}




//查找shop中对应id返回下标。
int  shop_id_query(char* str)
{
	for(int i=0; i<cnt_shop; i++)
	{
		if(0 == strcmp(shop[i].id,str))
    	{	
			return i;
    	}
	}
	return -1;
}

//查找shop中对应name返回下标。
int  shop_name_query(char* str)
{
	for(int i=0; i<cnt_shop; i++)
	{
		if(0 == strcmp(shop[i].name,str))
		{
			return i;
		}
	}
	return -1;
}

//查找logistics中对应id返回下标。
int  logistics_id_query(char* str)
{
	for(int i=0; i<cnt_logistics; i++)
	{
		if(0 == strcmp(logistics[i].id,str))
		{
			return i;
		}
	}
	return -1;
}

//查找logistics中对应password返回下标。
int  logistics_password_query(char* str)
{
	for(int i=0; i<cnt_logistics; i++)
	{
		if(0 == strcmp(logistics[i].password,str))
		{
			return i;
		}
	}
	return -1;
}

//查找user中对应id返回下标。
int  user_id_query(char* str)
{
	for(int i=0; i<cnt_user; i++)
	{
		if(0 == strcmp(user[i].id,str))
		{
			return i;
		}
	}
	return -1;
}

//查找user中对应password返回下标。
int  user_password_query(char* str)
{
	for(int i=0; i<cnt_user; i++)
	{
		if(0 == strcmp(user[i].password,str))
		{
			return i;
		}
	}
	return -1;
}


//存入数据
void wfp(void)
{
    //存储管理员信息
    FILE* manager_wfp=fopen("manager.txt","w");
    if(NULL == manager_wfp)
    {
		printf("写入数据错误！");
		return;
    }
	fprintf(manager_wfp,"%s %s %s %s \n",manager.id,manager.password,manager.phone,manager.number);
	fclose(manager_wfp);

    //存储商品信息。
    FILE* shop_wfp=fopen("shop.txt","w");
    if(NULL == shop_wfp)
    {
		printf("写入数据错误！");
		return;
    }
	for(int i=0;i<cnt_shop;i++)
	{
		fprintf(shop_wfp,"%s %s %lf %d %d\n",shop[i].id,shop[i].name,shop[i].price,shop[i].num,shop[i].loss);
	}
	fclose(shop_wfp);

    //存储用户信息。
    FILE* user_wfp=fopen("user.txt","w");
    if(NULL == user_wfp)
    {
		printf("写入数据错误！");
		return;
    }
	for(int i=0;i<cnt_user;i++)
	{
		fprintf(user_wfp,"%s %s %s %lf \n",user[i].id,user[i].password,user[i].phone,user[i].vip);
	}
	fclose(user_wfp);


    //存储后勤人员信息。

	char book_path[255];
	char book_isbn[15];
	get_str(book_isbn,sizeof(book_isbn));
	sprintf(book_path,"%s%s",BOOK_PATH,book_isbn);
    
	FILE* logistics_wfp=fopen(book_path,"w");
    if(NULL == logistics_wfp)
    {
		printf("写入数据错误！");
		return;
    }
	for(int i=0;i<cnt_logistics;i++)
	{
		fprintf(logistics_wfp,"%s %s %s \n",logistics[i].id,logistics[i].password,logistics[i].phone);
	}
	fclose(logistics_wfp);
   
    //存储日志信息。
    FILE* journal_wfp=fopen("journal.txt","w");
    if(NULL == journal_wfp)
    {
		printf("写入数据错误！");
		return;
    }
	for(int i=0;i<cnt_time;i++)
	{
		fprintf(journal_wfp,"%d %d %d %d %d %d %s %s %lf\n ",pay[i].year,pay[i].mon,pay[i].day,pay[i].hour,pay[i].min,pay[i].sec,pay[i].pay_phone,pay[i].collection_phone,pay[i].money);
	}
	fclose(journal_wfp);
}

//读取信息
void rfp(void)
{
    //读取管理员信息
	FILE* manager_rfp=fopen("manager.txt","r");
   	if(NULL == manager_rfp)
   	{
		printf("读取数据错误！");
		return;
	}
    fscanf(manager_rfp,"%s %s %s %s\n",manager.id,manager.password,manager.phone,manager.number);
    fclose(manager_rfp);


//读取商品信息
	FILE* shop_rfp=fopen("shop.txt","r");
   	if(NULL == shop_rfp)
   	{
		printf("读取数据错误！");
		return;
	}
    int shop_ret = 0;
    do{
		shop_ret = fscanf(shop_rfp,"%s %s %lf %d %d\n",shop[cnt_shop].id,shop[cnt_shop].name,&shop[cnt_shop].price,&shop[cnt_shop].num,&shop[cnt_shop].loss);
      }while(5== shop_ret &&++cnt_shop );
    fclose(shop_rfp);

//读取用户信息
	FILE* user_rfp=fopen("user.txt","r");
   	if(NULL == user_rfp)
   	{
		printf("读取数据错误！");
		return;
	}
    int user_ret = 0;
    do{
		user_ret = fscanf(user_rfp,"%s %s %s %lf\n",user[cnt_user].id,user[cnt_user].password,user[cnt_user].phone,&user[cnt_user].vip);
      }while(4 == user_ret && ++cnt_user);
    fclose(user_rfp);

//读取后勤人员信息
	FILE* logistics_rfp=fopen("/aa/logistics.txt","r");
   	if(NULL == logistics_rfp)
   	{
		printf("读取数据错误！");
		return;
	}
    int logistics_ret = 0;
    do{
		logistics_ret = fscanf(logistics_rfp,"%s %s %s \n",logistics[cnt_logistics].id,logistics[cnt_logistics].password,logistics[cnt_logistics].phone);
      }while(3 == logistics_ret&& ++cnt_logistics );
    fclose(logistics_rfp);

//读取日志信息。
    FILE* journal_rfp=fopen("journal.txt","r");
   	if(NULL == journal_rfp)
   	{
		printf("读取数据错误！");
		return;
	}
    int journal_ret = 0;
    do{
		journal_ret = fscanf(journal_rfp,"%d %d %d %d %d %d %s %s %lf \n",&pay[cnt_time].year,&pay[cnt_time].mon,&pay[cnt_time].day,&pay[cnt_time].hour,&pay[cnt_time].min,&pay[cnt_time].sec,pay[cnt_time].pay_phone,pay[cnt_time].collection_phone,&pay[cnt_time].money);
      }while(9== journal_ret && ++cnt_time);
    fclose(journal_rfp);
}

